<?php
namespace app\api\controller\notice;
use app\api\controller\Init;

class Notice extends Init
{
    public function initialize() {
        parent::initialize();
        $this->service = model('notice/Notice', 'service');
    }

    /**
     * 创建
     * @method create
     * @return [type] [description]
     */
    public function create()
    {

        $params['title'] = $this->request->post('title');
        $params['content'] = $this->request->post('content');
        $params['status'] = $this->request->post('status',1);
        $result = $this->service->create($params);
        if (!$result) {
            return $this->response($this->service->getError(),[],-300101);
        }
        return $this->response("success", $result);

    }

    /**
     * 修改
     * @method create
     * @return [type] [description]
     */
    public function edit()
    {
        $id = $this->request->post('id');
        $params = $this->request->post('params/a');
        $result = $this->service->edit($id,$params);
        if (!$result) {
            return $this->response($this->service->getError(),[],-300101);
        }
        return $this->response("success", $result);
    }

    /**
     * 删除
     * @method create
     * @return [type] [description]
     */
    public function del()
    {
        $id = $this->request->post('id');
        $result = $this->service->del($id);
        if (!$result) {
            return $this->response($this->service->getError(),[],-300101);
        }
        return $this->response("success", $result);
    }

    /**
     * 详情
     * @method create
     * @return [type] [description]
     */
    public function detail()
    {
        $id = $this->request->post('id');
        $result = $this->service->detail($id);
        if (!$result) {
            return $this->response($this->service->getError(),[],-300101);
        }
        return $this->response("success", $result);
    }

    /**
     * 列表
     * @method create
     * @return [type] [description]
     */
    public function lists()
    {
        $where = [];
        if(!empty($this->request->post('status')) ){
            $where[] = ['status','=',$this->request->post('status')];
        }
        if(!empty($this->request->post('title'))){
            $where[] = ['title|content','like', '%'.$this->request->post('title').'%'];
        }
        $attrs = $this->request->post('attrs/a',[]);
        $limit = $this->request->post('limit',12);
        $page = $this->request->post('page',1);
        $order = $this->request->post('order','id desc');
        $field = $this->request->post('field',true);
        $extra = $this->request->post('extra',[]);
        $funcs = $this->request->post('funcs',[]);

        $result = $this->service->list($where,$limit,$page,$order,$field,$attrs,$extra,$funcs);
        if (!$result) {
            return $this->response($this->service->getError(),[],-300101);
        }
        return $this->response("success", $result);
    }



}